package com.zyzo.demo.appengine.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;
import com.googlecode.objectify.util.DAOBase;

public class UserDao extends DAOBase 
{
	private static UserDao _instance=null ;
	protected UserDao()
	{
		ObjectifyService.register(User.class);
		
		/*init Premier user ?*/
		if( null == this.getUserByEmail("admin@mail.com") )
		{
			try
			{
				User admin = new User();
				admin.setId( (long)1  );
				admin.setEmail( "admin@mail.com" );
				admin.setPassword("admin");
				this.createUser( admin );
			}
			catch(Exception e)
			{
			    System.out.println(  "Unable to create First admin user ! "+ e.getLocalizedMessage() );
			}
		}
	}
	public static UserDao getInstance()
	{
		if(null==UserDao._instance)UserDao._instance = new UserDao();
		return UserDao._instance;
	}

	
	    public List<User> getAllUser( )
	    {
	    	return this.getAllUser(  "id" ,null, null , null ) ;
	    }
	    
	    public List<User> getAllUser( String sort ,String asc, String start , String length )
	    {
	    	/*
				private Long id;
				private String password;
				private String firstname;
				private String lastname;
				private String email;
				private String phone;
				private Date   subscriptionDate; 
	    	 */
	    	if( null == sort )sort = "id";
	    	if( null == asc )asc = "true";
	    	if( null == start ){start = "0";}
	    	int iStart = Integer.parseInt(start);
	    	
	    	if( ! Boolean.parseBoolean(asc) )sort="-"+sort; // " " : ascending, "-" : descending
	    	
	    	Query<User> q  = null;
	    	if( null == length )
	    	{
	    		q  = ofy().query( User.class ).order(sort).offset(iStart);
	    	}
	    	else
	    	{
	    		int iLength = Integer.parseInt(length);
	    		q  = ofy().query( User.class ).order(sort).offset(iStart).limit(iLength);
	    	}
	    	
	    	ArrayList<User> users = new ArrayList<User>();
	    	for (User u : q) users.add(u);
	    	
	    	return users;
	    }
	    
	    public User getUser( long id )
	    {
	    	User found = ofy().find( User.class, id );
	    	return found;
	    }

	    public User getUserByEmail( String mail )
	    {
	    	User u  = ofy().query( User.class).filter("email", mail ).get();
	    	return u;
	    }

	    
    
 	    public void updateUser(User user)  throws Exception
	    {
	    	User found = ofy().find( User.class, user.getId() );
	        if (found != null)
	        {
	        	ofy().delete( found );
	        	ofy().put( user );
	        }
	        else
		    {
		      	throw new Exception("No user with id "+user.getId()+" already exists.");
		    }
	        return  ;
	    }
	    
		public void createUser(User user) throws Exception
		{
	    	User found = ofy().find( User.class, user.getId() );
	        if (found == null)
	        {
	        	user.setSubscriptionDate( new Date() );
	        	ofy().put( user );
	        }
	        else
	        {
	        	throw new Exception("a user with id "+user.getId()+" already exists.");
	        }
	        return  ;
		}
		
		
		public void delete(long id) throws Exception
		{
	    	User found = ofy().find( User.class, id );
	        if (found == null)
	        {
	        	throw new Exception("No user with id "+id+" exists.");
	        }
	        else
	        {
	        	ofy().delete( found );
	        }
	        return  ;
		}
		
		
		public int countUsers()
		{
			return ofy().query( User.class).count();
		}
}